package org.example.edusmart.controller.Student;
import jakarta.servlet.http.HttpServletRequest;
import org.example.edusmart.request.TeaOrStu.CourseIdPojo;
import org.example.edusmart.service.CourseService;
import org.example.edusmart.utils.GetTokenInfo;
import org.example.edusmart.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import java.util.*;


@RestController
@RequestMapping("/StudentCourse")
public class StuCourseController {

    @Autowired
    private GetTokenInfo getTokenInfo;

    @Autowired
    private CourseService courseService;


    /**
     * 查看加入课程
     */
    @PostMapping("/get/")
    @PreAuthorize("hasAnyAuthority('student_permission', 'teacher_permission')")
    public Result getCourse(HttpServletRequest request) {
        String strId = getTokenInfo.getUserIdByJwt(request);
        //将String类型的数字字符串转换为int类型
        int userId = Integer.parseInt(strId);
        // 调用服务层方法获取课程列表
        List<HashMap<String, Object>> course = courseService.getCourseList(userId);
        return new Result(course, 200, "查询课程成功");
    }

    /**
     * 查看全部课程资料
     */
    @PostMapping("/getDirectory/")
    @PreAuthorize("hasAnyAuthority('student_permission', 'teacher_permission')")
    public Result getDirectory(@RequestBody CourseIdPojo course) {
        int courseId = course.getCourseId();
        List<HashMap<String, Object>> directory = courseService.getAllCourseDirectory(courseId);
        return new Result(directory, 200, "查询课程资料成功");
    }

    /**
     * 退出课程
     */
    @PostMapping("/exitCourse/")
    @PreAuthorize("hasAuthority('student_permission')")
    public Result exitCourse(HttpServletRequest request, @RequestBody CourseIdPojo course) {
        int courseId = course.getCourseId();
        String userId = getTokenInfo.getUserIdByJwt(request);
        int intUserId = Integer.parseInt(userId);
        courseService.exitCourse(intUserId, courseId);
        return new Result(200, "退出课程成功");
    }

    /**
     *获取教师联系方式
     */
    @PostMapping("/linkTeacher/")
    @PreAuthorize("hasAuthority('student_permission')")
    public Result exitCourse(@RequestBody CourseIdPojo course) {
        int courseId = course.getCourseId();
        String data = courseService.linkTeacher(courseId);
        return new Result(data, 200, "查询成功");
    }
}
